package com.usian.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.article.mapper.ApArticleMapper;
import com.usian.article.service.ApArticleConfigService;
import com.usian.article.service.ApArticleContentService;
import com.usian.article.service.ApArticleService;
import com.usian.model.article.dtos.ArticleHomeDto;
import com.usian.model.article.dtos.ArticleInfoDto;
import com.usian.model.article.pojos.ApArticle;
import com.usian.model.article.pojos.ApArticleConfig;
import com.usian.model.article.pojos.ApArticleContent;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.jcajce.provider.symmetric.AES;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Value("${fileServerUrl}")
    private String fileServerUrl;

    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private ApArticleConfigService apArticleConfigService;

    @Autowired
    private ApArticleContentService apArticleContentService;


    @Override
    public ApArticle saveArticle(ApArticle apArticle) {
        int insert = apArticleMapper.insert(apArticle);
        return apArticle;
    }

    @Override
    public ResponseResult findArcitle(ArticleHomeDto dto) {

        //1：校验size条数
        Integer size = dto.getSize();
        if (size == null || size != 10) {
            size = 10;
        }
        dto.setSize(size);

        //2:判断默认页1  上拉更多1   下拉最新2
        if (dto.getType() == null || (dto.getType() != 1 && dto.getType() != 2)) {
            dto.setType(1);
        }

        //3：时间判断
        if (dto.getMaxBehotTime() == null) {
            dto.setMaxBehotTime(new Date());
        }
        if (dto.getMinBehotTime() == null) {
            dto.setMinBehotTime(new Date());
        }
        //4:频道
        /*if(dto.getTag().equals("__all__")){
            dto.setTag("_all_");
        }*/

        List<ApArticle> articleList = apArticleMapper.findArticle(dto);

        ResponseResult responseResult = ResponseResult.okResult(articleList);
        responseResult.setHost(fileServerUrl);
        return responseResult;
    }

    @Override
    public ResponseResult findArticleInfo(ArticleInfoDto dto) {

        /**
         * 1:校验参数
         * 2：查询config 和 content 放到一个hashmao集合
         * 3；结果统一封装
         *
         */

        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        }

        Map<String, Object> result = new HashMap<>();

        ApArticleConfig apArticleConfig = apArticleConfigService.getOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, dto.getArticleId()));
        if(apArticleConfig == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        result.put("config", apArticleConfig);

        ApArticleContent apArticleContent = apArticleContentService.getOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, dto.getArticleId()));
        if(apArticleContent == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        result.put("content", apArticleContent);

        return ResponseResult.okResult(result);
    }



}
